var orderCache = require('../../../utils/mr_xu/apply_order_cache.js') 
var common = require('../../../utils/mr_xu/common.js')

Page({

  data: {
    select: 0,
    list: []
  },


  select(e) {
    let index = e.currentTarget.dataset.index
    console.log(index)
    
    if (!this.data.list[index].enable) {
      return
    }

    this.setData({
      select: index
    })
  },

  next() {

    let that = this

    common.safeRun(function() {
      orderCache.setItem('business', that.data.list[that.data.select].value)

      if (that.data.select == 0 || that.data.select == 2) {
        wx.navigateTo({
          url: '../step5_2_select_car_address/select_car_address',
        })
      } else {
        wx.navigateTo({
          url: '../step5_1_more_clue/more_clue',
        })
      }
    })
  },

  onLoad: function (options) {
    console.log('type')
    console.log(orderCache.getItem('type'))

    
    if (options.isGps === "true") {
      var items = [
        { name: '将车送到指定地点', value: 1 ,enable:true},
        { name: '仅找车', value: 2, enable:false },
        { name: '找车且送到自定地点', value: 3, enable:false }
      ]
    } else {
      var items = [
        { name: '将车送到指定地点', value: 1, enable: false },
        { name: '仅找车', value: 2, enable: true },
        { name: '找车且送到自定地点', value: 3, enable: true }
      ]
    }

    

    this.setData({
      list: items,
      isGps: options.isGps === "false" ? false : true,
      select: options.isGps === "false" ? 1 :0
    })

    let that = this
    let updateInfo = orderCache.getUpdateInfo()
    if (updateInfo) {
      let index = 0
      for (let i in items) {
        if (items[i].value == updateInfo.business) {
          index = i
          break
        }
      }

      if (options.isGps === "true") {
        if (index != 0) {
          index = 0
        }
      } else {
        if (index == 0) {
          index = 1
        }
      }
      that.setData({
        select: index
      })
    }
  },

})